<?php
App::uses('AppController', 'Controller');
CakePlugin::load('Gdata2');
class DocumentsController extends AppController {
	public $name = 'Documents';
	public $uses = array('Document');

	public function index() {
		//debug($_SESSION);
		if($_SESSION['UserAuth']['User']['user_group_id'] == 1){
			$this->Document->Behaviors->unload('SoftDelete');
		}
		$this->paginate = array(
			'order' => array('Document.release_date'=>'desc'),
			'limit' => 2
		);
		$this->set('documents', $this->paginate('Document'));
		//debug($this->paginate('Document'));
	}
	
	public function add() {
		//Configure::write('debug', 2);
		$this->layout = 'ajax';
		if(!empty($this->data)){
			//echo '<pre>';
			//print_r($this->data);
			if(!empty($this->data['Document']['files'])){
				$file = $this->data['Document']['files'];
				$file_name = str_replace(" ","_",strtotime('now').'_'.$this->data['Document']['files']['name']);
				$tmp_name = $this->data['Document']['files']['tmp_name'];
				$mime = $this->data['Document']['files']['type'];
				//if($mime == 'image/jpg' || $mime == 'image/jpeg' || $mime == 'image/png' || $mime == 'image/gif'){
					$dir = WWW_ROOT.'files'.DS.'documents'.DS;
					if(move_uploaded_file($tmp_name, $dir . $file_name)){
						$document['Document']['file'] = $file_name;
						$document['Document']['mime'] = $mime;
						$document['Document']['name'] = $this->data['Document']['name'];
						$document['Document']['serial'] = $this->data['Document']['serial'];
						$document['Document']['note'] = $this->data['Document']['note'];
						$document['Document']['owner'] = $this->data['Document']['owner'];
						$document['Document']['user_id'] = $_SESSION['UserAuth']['User']['id'];
						$document['Document']['release_date'] = $this->data['Document']['release_date'];
						//print_r($this->data['Document']);
						$this->Document->create();
						if($this->Document->save($document)){
							$this->Document->uploadToGoogle($this->Document->id,$dir.$file_name);
							
							$this->Session->write('status', 'success');
							$this->Session->setFlash("<strong>Well done!</strong> You successfully save data.");
						}
					}
					//$this->Session->setFlash("Foto telah diupload", "success");
				//}else{
					//$this->Session->setFlash("Gagal, foto harus dalam format jpg, png atau gif", "error");
				//}
				//$this->redirect(array('action'=>'detail',$user_id));
			}else{
				$this->Session->write('status', 'error');
				$this->Session->setFlash("<strong>Sorry!</strong> You must include a file to upload.");
			}
			//echo '</pre>';
			$this->redirect(array('action'=>'index'));
		}
	}
	
	public function edit($id) {
		$this->layout = 'ajax';
		if($_SESSION['UserAuth']['User']['user_group_id'] == 1){
			$this->Document->Behaviors->unload('SoftDelete');
		}
		if(!empty($this->data)){
			//echo '<pre>';
			//print_r($this->data);
			if(!empty($this->data['Document']['files']['name'])){
				//echo 'pake file';
				$file = $this->data['Document']['files'];
				$file_name = str_replace(" ","_",strtotime('now').'_'.$this->data['Document']['files']['name']);
				$tmp_name = $this->data['Document']['files']['tmp_name'];
				$mime = $this->data['Document']['files']['type'];
				//if($mime == 'image/jpg' || $mime == 'image/jpeg' || $mime == 'image/png' || $mime == 'image/gif'){
					$dir = WWW_ROOT.'files'.DS.'documents'.DS;
					if(move_uploaded_file($tmp_name, $dir . $file_name)){
						$document['Document']['id'] = $this->data['Document']['id'];
						$document['Document']['file'] = $file_name;
						$document['Document']['mime'] = $mime;
						$document['Document']['name'] = $this->data['Document']['name'];
						$document['Document']['serial'] = $this->data['Document']['serial'];
						$document['Document']['note'] = $this->data['Document']['note'];
						$document['Document']['owner'] = $this->data['Document']['owner'];
						$document['Document']['user_id'] = $_SESSION['UserAuth']['User']['id'];
						$document['Document']['release_date'] = $this->data['Document']['release_date'];
						//print_r($this->data['Document']);
						if($this->Document->save($document)){
							$this->Document->uploadToGoogle($this->Document->id,$dir.$file_name);
							
							$this->Session->write('status', 'success');
							$this->Session->setFlash("<strong>Well done!</strong> You successfully save data.");
						}
					}
					//$this->Session->setFlash("Foto telah diupload", "success");
				//}else{
					//$this->Session->setFlash("Gagal, foto harus dalam format jpg, png atau gif", "error");
				//}
				//$this->redirect(array('action'=>'detail',$user_id));
			}else{
				//echo 'ga pake file';
				$document['Document']['id'] = $this->data['Document']['id'];
				$document['Document']['name'] = $this->data['Document']['name'];
				$document['Document']['serial'] = $this->data['Document']['serial'];
				$document['Document']['note'] = $this->data['Document']['note'];
				$document['Document']['owner'] = $this->data['Document']['owner'];
				$document['Document']['user_id'] = $_SESSION['UserAuth']['User']['id'];
				$document['Document']['release_date'] = $this->data['Document']['release_date'];
				if($this->Document->save($document)){
					$this->Session->write('status', 'success');
					$this->Session->setFlash("<strong>Well done!</strong> You successfully save data.");
				}else{
					$this->Session->write('status', 'error');
					$this->Session->setFlash("<strong>Sorry!</strong> Can't edit data, please try again.");
				}
			}
			//echo '</pre>';
			$this->redirect(array('action'=>'index'));
		}
		$document = $this->Document->find('first',array(
			'conditions'=>array('Document.id'=>$id)
		));
		//print_r($document);
		$this->set('id', $id);
		$this->set('document', $document);
	}
	
	public function deleteConfirm($id) {
		$this->layout = 'ajax';
		$this->set('id', $id);
	}
	
	public function delete($id) {
		if($this->Document->delete($id)){
			$this->Session->write('status', 'success');
			$this->Session->setFlash("<strong>Well done!</strong> You successfully delete data.");
		}else{
			//$this->Session->write('status', 'error');
			//$this->Session->setFlash("<strong>Oh snap!</strong> Can't delete data, please try again.");
			$this->Session->write('status', 'success');
			$this->Session->setFlash("<strong>Well done!</strong> You successfully delete data.");
		}
		$this->redirect($this->referer());
	}
	
	function archive($id){
		$this->layout = 'ajax';
		$this->set('id', $id);
		if(!empty($this->data)){
			$this->Document->id = $id;
			$this->Document->saveField('archive_date',$this->data['Document']['archive_date']);
			$this->Session->write('status', 'success');
			$this->Session->setFlash("<strong>Well done!</strong> You successfully archive selected data.");
			$this->redirect($this->referer());
		}
	}
	
	function searchFile($max='10'){
		//Configure::write('debug', 2);
		$q = $this->data['Document']['q'];
		$params = array('xoauth_requestor_id' => 'its@sbm-itb.ac.id');
		$search = ClassRegistry::init('Gdata2.Doc')->search($params,$q,$max);
		if(isset($search['feed'])){
			if(isset($search['feed']['entry']['id'])){
				$files = array($search['feed']['entry']);
			}else{
				$files = $search['feed']['entry'];
			}
		}
		//echo '<pre>';
		//print_r($files);
		//echo '</pre>';die();
		//debug($files);die();
		$arr_files = array();
		$resource_id = array();
		$title = array();
		foreach($files as $k=>$v){
			//$files[$k] = $v;
			$title[] = $v['title'];
			$resource_id[] = $v['gd:resourceId'];
			foreach($v['link'] as $link){
				if($link['@rel'] == 'edit'){
					$url = $link['@href'];
					$prevent = ClassRegistry::init('Gdata2.Doc')->preventDownload($params,$v['gd:resourceId']);
					//debug($prevent);
				}
			}
/*
			$document = ClassRegistry::init('Document')->find('first',array(
				'conditions'=>array('Document.gid'=>$resource_id),
				'contain'=>array(
					'User'
				)
			));
			//debug($document);
			if(!empty($document)){
				$arr_files[$k] = $v;
				$arr_files[$k]['Document'] = $document['Document'];
				$arr_files[$k]['User'] = $document['User'];
			}
*/
		}
		//debug($title);die();
		if($_SESSION['UserAuth']['User']['user_group_id'] == 1){
			$this->Document->Behaviors->unload('SoftDelete');
		}
		$documents = ClassRegistry::init('Document')->find('all',array(
			'conditions'=>array('Document.file'=>$title),
			'contain'=>array(
				'User'
			)
		));
		//echo '<pre>';
		//print_r($documents);
		//echo '</pre>';
		$this->set('documents',$documents);
		$this->set('max',$max);
		$this->set('q',$q);
		//debug($q);
	}
}
